<?php

class admin_groups_controller {
    
    public function exec_index(){
        //This function return the list of users
        global $dbdefault;
        $page = GET_value('p',1);
        $query = "SELECT * FROM groups";
        $result = mysql_query($query,$dbdefault) or die(__CLASS__."->".__METHOD__.": ".mysql_err());
        $groups=array();
        while($r = mysql_fetch_assoc($result)){
            $groups[]=$r;
        }
        $tpl = new Template(MODULE_ROOT.'/admin_groups/view/index.html');
        $tpl ->set('groups', $groups);
        $tpl ->set('page', $page);
        $content = $tpl ->fetch();
        return array('content'=>$content);
    }

    public function exec_create(){
        //This function create a new users
        $status = GET_value('s');
        $data['group_name'] = strtolower (POST_value('group_name',''));
        $data['group_description'] = POST_value('group_description','');
        if ('submit' == $status){
            //Do insert stuff
            $r = Group :: create($data);
            if ($r['success']){
                $content='<div class="success" style="margin:10px">Group '.$data['group_name'].' is successfully created</div>';
                $index = $this->exec_index();
                $content.=$index['content'];
            }else{
                $content = '';
                if (!empty($r['error'])){ 
                    $content = '<div class="error" style="margin:10px">'.$r['error'].'</div>';
                }
                $content .= $this->show_create_form($data, $r['error']);
            }
        }else{
            //Just show form
            $content = $this->show_create_form($data, null);
        }
        
        return array('content'=>$content);
    }
//
    public function exec_delete(){
        $group_id = (int) GET_value('group_id',0);
        $r = Group :: delete($group_id);
        if ($r['success']){
            $content = '<div class="success">Delete user successful!</div>';
            $index = $this->exec_index();
            $content.=$index['content']; 
        }else{
            $content='';
            if (isset($r['error'])) $content .= '<div class="error">Something wrong! '.$r['error'].'</div>';
            $index = $this->exec_index();
            $content.=$index['content']; 
        }
        return array('content' => $content);
    }

    public function exec_edit(){
        $status = GET_value('s');
        $group_id = (int) GET_value('group_id',0);
        if ('submit' == $status){
            $data['group_name'] = POST_value('group_name','');
            $data['group_description'] = POST_value('group_description','');
            $r = Group :: update($group_id,$data);
            if ($r['success']){
                $content = '<div class="success">Group #'.$group_id.' is updated successfully!</div>';
                $index = $this->exec_index();
                $content.=$index['content']; 
            }else{
                $content='';
                //print_r($r);
                if (isset($r['error'])) $content .= '<div class="error">Something wrong! '.$r['error'].'</div>';
                $content .= $this->show_edit_form($data, $r['error']);
            }
        }else{
            $r = Group :: get_group_data($group_id);
            if ($r['success']){
                $content = $this->show_edit_form($r['data'], '');
            }else{
                $content = "<div class='error'>".$r['error'].'</div>';
            }
        }
        return array('content' => $content);
    }

    private function show_create_form($data,$error){
        $tpl = new Template(MODULE_ROOT.'/admin_groups/view/create_form.html');
        $tpl ->set('data', $data);
        $tpl ->set('error', $error);
        return $tpl ->fetch();
    }
    
    private function show_edit_form($data,$error){
        $tpl = new Template(MODULE_ROOT.'/admin_groups/view/edit_form.html');
        $tpl ->set('data', $data);
        $tpl ->set('error', $error);
        return $tpl ->fetch();
    }

    /* The below handle groups permission*/
    public function exec_permissions(){
        $group_id = GET_value('group_id');
        $status = GET_value('status','');
        $modules_permisions = Modules::get_modules_permissions();
        $default_permissions = Modules::get_default_permissions();
        
        $content = '';
        if ('submit'==$status){
            foreach($default_permissions as $per => $v){
                $permissions[$per] = isset($_POST[$per])? 1 : 0;
            }
            $r = Group :: set_permissions($group_id,$permissions);
            if ($r['success']){
                $content .= '<div class="success">Permissions has been set succesfully!</div>';
            }else{
                $content .= '<div class="success">'.$r['error'].'</div>';                
            }
            
        }else{
            //
            //Real permissions (must compare with DB values)
            $r = Group :: get_permissions($group_id);
            if ($r['success']){
                $permissions = $r['permissions']; //Need to merge with default_permission, incase modules change
                //print_r($permissions);
            }else{
                $content .='<div class="info">'.$r['error'].'</div>';
                $permissions = $default_permissions;                            
            }
        }
        
        
        //Show form
        $tpl = new Template(MODULE_ROOT.'/admin_groups/view/permissions_form.html');
        $tpl ->set('permissions', $permissions);
        $tpl ->set('modules_permissions', $modules_permisions);
        $tpl ->set('group_id', $group_id);
        $content .= $tpl ->fetch();
        return array('content'=>$content);
    }

    
}